<template>
  <div class="body-part-selector">
    <div class="selector-header">
      <h3 class="title">
        <i class="fas fa-user-md"></i>
        请选择症状部位
      </h3>
      <p class="subtitle">点击人体图上的相应部位</p>
    </div>

    <div class="body-diagram-container">
      <!-- 人体正面图 -->
      <div class="body-diagram front-view" :class="{ active: viewMode === 'front' }">
        <svg viewBox="0 0 300 500" class="body-svg">
          <!-- 头部 -->
          <ellipse 
            cx="150" cy="60" rx="40" ry="50"
            :class="getPartClass('head')"
            @click="selectPart('head', '头部')"
          />
          
          <!-- 颈部 -->
          <rect 
            x="135" y="105" width="30" height="25"
            :class="getPartClass('neck')"
            @click="selectPart('neck', '颈部')"
          />
          
          <!-- 胸部 -->
          <ellipse 
            cx="150" cy="170" rx="60" ry="40"
            :class="getPartClass('chest')"
            @click="selectPart('chest', '胸部')"
          />
          
          <!-- 腹部 -->
          <ellipse 
            cx="150" cy="240" rx="55" ry="35"
            :class="getPartClass('abdomen')"
            @click="selectPart('abdomen', '腹部')"
          />
          
          <!-- 左臂 -->
          <ellipse 
            cx="90" cy="180" rx="20" ry="60"
            :class="getPartClass('left_arm')"
            @click="selectPart('left_arm', '左臂')"
          />
          
          <!-- 右臂 -->
          <ellipse 
            cx="210" cy="180" rx="20" ry="60"
            :class="getPartClass('right_arm')"
            @click="selectPart('right_arm', '右臂')"
          />
          
          <!-- 左腿 -->
          <ellipse 
            cx="125" cy="350" rx="25" ry="80"
            :class="getPartClass('left_leg')"
            @click="selectPart('left_leg', '左腿')"
          />
          
          <!-- 右腿 -->
          <ellipse 
            cx="175" cy="350" rx="25" ry="80"
            :class="getPartClass('right_leg')"
            @click="selectPart('right_leg', '右腿')"
          />
        </svg>
      </div>

      <!-- 人体背面图 -->
      <div class="body-diagram back-view" :class="{ active: viewMode === 'back' }">
        <svg viewBox="0 0 300 500" class="body-svg">
          <!-- 头部后面 -->
          <ellipse 
            cx="150" cy="60" rx="40" ry="50"
            :class="getPartClass('head_back')"
            @click="selectPart('head_back', '头部后面')"
          />
          
          <!-- 颈部后面 -->
          <rect 
            x="135" y="105" width="30" height="25"
            :class="getPartClass('neck_back')"
            @click="selectPart('neck_back', '颈部后面')"
          />
          
          <!-- 背部上方 -->
          <ellipse 
            cx="150" cy="170" rx="60" ry="40"
            :class="getPartClass('upper_back')"
            @click="selectPart('upper_back', '背部上方')"
          />
          
          <!-- 腰部 -->
          <ellipse 
            cx="150" cy="240" rx="55" ry="35"
            :class="getPartClass('lower_back')"
            @click="selectPart('lower_back', '腰部')"
          />
        </svg>
      </div>
    </div>

    <!-- 视图切换按钮 -->
    <div class="view-toggle">
      <el-button-group>
        <el-button 
          :type="viewMode === 'front' ? 'primary' : 'default'"
          @click="viewMode = 'front'"
          size="small"
        >
          <i class="fas fa-user"></i>
          正面
        </el-button>
        <el-button 
          :type="viewMode === 'back' ? 'primary' : 'default'"
          @click="viewMode = 'back'"
          size="small"
        >
          <i class="fas fa-user"></i>
          背面
        </el-button>
      </el-button-group>
    </div>

    <!-- 已选择的部位列表 -->
    <div class="selected-parts" v-if="selectedParts.length > 0">
      <h4>已选择的部位：</h4>
      <div class="parts-list">
        <el-tag 
          v-for="part in selectedParts" 
          :key="part.id"
          closable
          @close="removePart(part.id)"
          type="success"
          class="part-tag"
        >
          {{ part.name }}
        </el-tag>
      </div>
    </div>

    <!-- 常见部位快捷选择 -->
    <div class="quick-select">
      <h4>常见症状部位：</h4>
      <div class="quick-buttons">
        <el-button 
          v-for="part in commonParts" 
          :key="part.id"
          size="small"
          @click="selectPart(part.id, part.name)"
          :type="isPartSelected(part.id) ? 'success' : 'default'"
        >
          <i :class="part.icon"></i>
          {{ part.name }}
        </el-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { ElButton, ElButtonGroup, ElTag } from 'element-plus'

// Props
const props = defineProps({
  modelValue: {
    type: Array,
    default: () => []
  }
})

// Emits
const emit = defineEmits(['update:modelValue', 'select'])

// 响应式数据
const viewMode = ref('front')
const selectedParts = ref([...props.modelValue])

// 常见部位快捷选择
const commonParts = ref([
  { id: 'head', name: '头部', icon: 'fas fa-head-side-cough' },
  { id: 'chest', name: '胸部', icon: 'fas fa-lungs' },
  { id: 'abdomen', name: '腹部', icon: 'fas fa-stomach' },
  { id: 'back', name: '背部', icon: 'fas fa-spine' },
  { id: 'throat', name: '咽喉', icon: 'fas fa-head-side-cough' },
  { id: 'joints', name: '关节', icon: 'fas fa-bone' }
])

// 计算属性
const isPartSelected = computed(() => {
  return (partId) => {
    return selectedParts.value.some(part => part.id === partId)
  }
})

// 方法
const getPartClass = (partId) => {
  const baseClass = 'body-part'
  const isSelected = isPartSelected.value(partId)
  return {
    [baseClass]: true,
    'selected': isSelected,
    'hoverable': !isSelected
  }
}

const selectPart = (partId, partName) => {
  const existingIndex = selectedParts.value.findIndex(part => part.id === partId)
  
  if (existingIndex === -1) {
    // 添加新选择的部位
    const newPart = { id: partId, name: partName }
    selectedParts.value.push(newPart)
  } else {
    // 如果已选择，则取消选择
    selectedParts.value.splice(existingIndex, 1)
  }
  
  // 发出事件
  emit('update:modelValue', selectedParts.value)
  emit('select', selectedParts.value)
}

const removePart = (partId) => {
  const index = selectedParts.value.findIndex(part => part.id === partId)
  if (index !== -1) {
    selectedParts.value.splice(index, 1)
    emit('update:modelValue', selectedParts.value)
    emit('select', selectedParts.value)
  }
}

// 监听props变化
watch(() => props.modelValue, (newValue) => {
  selectedParts.value = [...newValue]
}, { deep: true })
</script>

<style scoped>
.body-part-selector {
  padding: 20px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.selector-header {
  text-align: center;
  margin-bottom: 20px;
}

.title {
  color: #2c5aa0;
  font-size: 18px;
  font-weight: 600;
  margin: 0 0 8px 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.subtitle {
  color: #666;
  font-size: 14px;
  margin: 0;
}

.body-diagram-container {
  position: relative;
  display: flex;
  justify-content: center;
  margin: 20px 0;
  min-height: 400px;
}

.body-diagram {
  position: absolute;
  opacity: 0;
  transform: scale(0.95);
  transition: all 0.3s ease;
}

.body-diagram.active {
  opacity: 1;
  transform: scale(1);
}

.body-svg {
  width: 250px;
  height: 400px;
  cursor: pointer;
}

.body-part {
  fill: #e8f4fd;
  stroke: #2c5aa0;
  stroke-width: 2;
  transition: all 0.3s ease;
  cursor: pointer;
}

.body-part.hoverable:hover {
  fill: #d1e9ff;
  stroke: #1e4a8c;
  stroke-width: 3;
  transform: scale(1.05);
}

.body-part.selected {
  fill: #4CAF50;
  stroke: #2e7d32;
  stroke-width: 3;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0% { opacity: 1; }
  50% { opacity: 0.7; }
  100% { opacity: 1; }
}

.view-toggle {
  display: flex;
  justify-content: center;
  margin: 20px 0;
}

.selected-parts {
  margin: 20px 0;
  padding: 15px;
  background: #f8f9fa;
  border-radius: 8px;
  border-left: 4px solid #4CAF50;
}

.selected-parts h4 {
  margin: 0 0 10px 0;
  color: #2c5aa0;
  font-size: 14px;
  font-weight: 600;
}

.parts-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.part-tag {
  margin: 0;
}

.quick-select {
  margin-top: 20px;
  padding: 15px;
  background: #f8f9fa;
  border-radius: 8px;
  border-left: 4px solid #2c5aa0;
}

.quick-select h4 {
  margin: 0 0 12px 0;
  color: #2c5aa0;
  font-size: 14px;
  font-weight: 600;
}

.quick-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.quick-buttons .el-button {
  margin: 0;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .body-part-selector {
    padding: 15px;
  }
  
  .body-svg {
    width: 200px;
    height: 320px;
  }
  
  .quick-buttons {
    justify-content: center;
  }
  
  .quick-buttons .el-button {
    font-size: 12px;
    padding: 6px 12px;
  }
}
</style>